<template>
  <div>
    <!-- immediate-check 阻止默认触发 -->
    <van-list
      v-model="loading"
      :immediate-check="false"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <commentItem v-for="item in list" :key="item.com_id+''"
        :row="item"
        @clickReply="$emit('clickReply',item)"
      />
    </van-list>
  </div>
</template>

<script>
import { getCommentList } from '@/api/article.js'
import commentItem from './commentItem.vue'
export default {
  props: {
    // 获取评论列表类型
    type: {
      type: String,
      default: 'a' // 默认值
    },
    // 可能是文章id 或 评论id
    source: {
      type: [Number, Object, String],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {
    this.onLoad()
  },

  methods: {
    async onLoad () {
      // 发送请求
      const res = await getCommentList({
        type: this.type,
        source: this.source + '',
        offset: this.offset,
        limit: this.limit
      })
      this.$emit('updateTotal', res.total_count)
      // 保存数据
      this.list.push(...res.results)
      // 停止当前的loading
      this.loading = false
      // 判断是否全部加载完成
      if (!res.last_id || this.list.length >= res.total_count) {
        this.finished = true
      } else {
        this.offset = res.last_id
      }
    }
  },
  components: {
    commentItem
  }
}
</script>

<style scoped>

</style>
